/*
    webpack配置
*/
//引入路径管理模块
const path = require('path');

//引入html打包插件
const HtmlWebpackPlugin = require('html-webpack-plugin')

//提取css
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
//压缩css
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')


//查看当前是哪种模式
console.log('当前模式', process.env.NODE_ENV);

//各种配置  暴露出去
module.exports = {
    //入口 (单个入口 或多个入口)
    entry: {
        //配置分用样式
        commCss: './src/less/comm/index.js',

        //公用js配置
        dom: './src/js/comm/dom.js',
        http: './src/js/comm/http.js',
        utils: './src/js/comm/utils.js',
        code: './src/lib/captcha/captcha-mini.js',
        swiper:'./src/lib/swiper/swiper-bundle.js',
        weui:'./src/lib/weui/weui.js',

        //配置每一个页面的入口
        advertisement: './src/js/advertisement.js',
        register: './src/js/register.js',
        login: './src/js/login.js',
        home: './src/js/home.js',
        my: './src/js/my.js',
        infor: './src/js/infor.js',
        Medal: './src/js/Medal.js',
        run: './src/js/run.js',
        Course: './src/js/Course.js',
        Cycling: './src/js/Cycling.js',
        introduction: './src/js/introduction.js',
        coursetraining: './src/js/coursetraining.js',
        sportDate: './src/js/sportDate.js',
        
    },
    //出口
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'js/[name]-[hash:10].js',//多出口 
        publicPath: './'
    },
    //加载器
    module: {
        rules: [
            //css
            {
                test: /\.css$/,
                use: [
                    //配置插件到加载器规则中
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            publicPath: '../'
                        }
                    }
                    , 'css-loader',
                    'postcss-loader']
            },
            //less
            {
                test: /\.less$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            publicPath: '../'
                        }
                    }
                    , 'css-loader',
                    'postcss-loader', // 处理css兼容性
                    'less-loader']
            },
            //css中图像
            {
                test: /\.(jpg|png|gif)$/,
                loader: 'url-loader',
                options: {
                    name: '[hash:10].[ext]',
                    limit: 10 * 1024,
                    outputPath: 'imgs',//把图像打包到指定目录
                    esModule: false
                }
            },
            //html图像
            {
                test: /\.html$/,
                loader: 'html-loader',
            },
            //字体图标
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,//处理的格式
                loader: 'file-loader',
                options: {
                    name: '[hash:10].[ext]',//输出文件名设置
                    outputPath: 'fonts'//输出目录
                }
            },
            //es6转es5
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/,//不要处理node_modules
            },

        ]
    },
    plugins: [
        //多个html页面
        new HtmlWebpackPlugin({
            template: './src/page/advertisement.html',//把那个html文件打包到dist目录中
            filename: 'advertisement.html',//输出什么名字
            chunks: ['advertisement', 'commCss', 'dom', 'utils'],// 当前页面所需要哪些 模块 模块引入顺序和入口设置时的先后有关
        }),
        new HtmlWebpackPlugin({
            template: './src/page/register.html',//把那个html文件打包到dist目录中
            filename: 'register.html',//输出什么名字
            chunks: ['register', 'commCss', 'dom', 'utils', 'http',"code"]
        }),
        new HtmlWebpackPlugin({
            template: './src/page/login.html',//把那个html文件打包到dist目录中
            filename: 'login.html',//输出什么名字
            chunks: ['login', 'commCss', 'dom', 'utils', 'http']
        }),
        new HtmlWebpackPlugin({
            template: './src/page/home.html',//把那个html文件打包到dist目录中
            filename: 'home.html',//输出什么名字
            chunks: ['home', 'commCss', 'dom', 'utils', 'http','swiper']
        }),
        new HtmlWebpackPlugin({
            template: './src/page/my.html',//把那个html文件打包到dist目录中
            filename: 'my.html',//输出什么名字
            chunks: ['my', 'commCss', 'dom', 'utils', 'http']
        }),
        new HtmlWebpackPlugin({
            template: './src/page/infor.html',//把那个html文件打包到dist目录中
            filename: 'infor.html',//输出什么名字
            chunks: ['infor', 'commCss', 'dom', 'utils', 'http','weui']
        }),
        new HtmlWebpackPlugin({
            template: './src/page/Medal.html',//把那个html文件打包到dist目录中
            filename: 'Medal.html',//输出什么名字
            chunks: ['Medal', 'commCss', 'dom', 'utils', 'http']
        }),
        new HtmlWebpackPlugin({
            template: './src/page/run.html',//把那个html文件打包到dist目录中
            filename: 'run.html',//输出什么名字
            chunks: ['run', 'commCss', 'dom', 'utils', 'http']
        }),
        new HtmlWebpackPlugin({
            template: './src/page/Course.html',//把那个html文件打包到dist目录中
            filename: 'Course.html',//输出什么名字
            chunks: ['Course', 'commCss', 'dom', 'utils', 'http']
        }),
        new HtmlWebpackPlugin({
            template: './src/page/Cycling.html',//把那个html文件打包到dist目录中
            filename: 'Cycling.html',//输出什么名字
            chunks: ['Cycling', 'commCss', 'dom', 'utils', 'http']
        }),
        new HtmlWebpackPlugin({
            template: './src/page/introduction.html',//把那个html文件打包到dist目录中
            filename: 'introduction.html',//输出什么名字
            chunks: ['introduction', 'commCss', 'dom', 'utils', 'http']
        }),

        /* 课程训练 */
        new HtmlWebpackPlugin({
            template: './src/page/coursetraining.html',//把那个html文件打包到dist目录中
            filename: 'coursetraining.html',//输出什么名字
            chunks: ['coursetraining', 'commCss', 'dom', 'utils', 'http']
        }),
        new HtmlWebpackPlugin({
            template: './src/page/sportDate.html',//把那个html文件打包到dist目录中
            filename: 'sportDate.html',//输出什么名字
            chunks: ['sportDate', 'commCss', 'dom', 'utils', 'http']
        }),

        

        //提取css
        new MiniCssExtractPlugin({
            filename: 'css/[name].css' //设置css输出的文件名

        }),
        //压缩css
        new OptimizeCssAssetsWebpackPlugin()
    ],
    //模式   development  production
    mode: process.env.NODE_ENV == 'dev' ? 'development' : 'production',
    //开发服务器
    devServer: {
        contentBase: path.resolve(__dirname, 'dist'),
        publicPath: '/',//服务器根目录
        compress: true, // 启动gzip
        port: 1001,  // 端口
        open: true, // 自动打开服务
        openPage: 'advertisement.html'//打开哪个页面
    }
}